"use server"
import { Flex } from "antd"
import Link from "next/link"
import { Divider } from "antd"
import Title from "antd/es/typography/Title"
import QuestionBankList from "@/components/question-bank-list"
import QuestionList from "@/components/question-list"
import { listQuestionBankVOByPage } from "@/api/question-bank"
import { listQuestionVOByPage } from "@/api/question"
import "./index.css"

export default async function HomePage() {
  let questionBankList: QuestionBankVO[] = []
  let questionList: QuestionVO[] = []

  try {
    const res = await listQuestionBankVOByPage({
      pageSize: 12,
      sortField: "createTime",
      sortOrder: "descend"
    })
    questionBankList = res.data.records ?? []
  } catch (error) {
    console.error("获取题库失败，" + error)
  }

  try {
    const res = await listQuestionVOByPage({
      pageSize: 12,
      sortField: "createTime",
      sortOrder: "descend"
    })
    questionList = res.data.records ?? []
  } catch (error) {
    console.error("获取题目失败，" + error)
  }

  return (
    <div id="home-page" className="max-width-content">
      <Flex justify="space-between" align="center">
        <Title level={3}>最新题库</Title>
        <Link href="/banks">查看更多</Link>
      </Flex>
      <QuestionBankList questionBankList={questionBankList} />
      <Divider />
      <Flex justify="space-between" align="center">
        <Title level={3}>最新题目</Title>
        <Link href="/questions">查看更多</Link>
      </Flex>
      <QuestionList questionList={questionList} />
    </div>
  )
}
